package com.zxl.always.on.display.configs.bean;

import android.text.TextUtils;

import com.zxl.always.on.display.ApplicationHelper;
import com.zxl.always.on.display.tools.AppUtils;
import com.zxl.always.on.display.tools.LogUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import static com.zxl.always.on.display.configs.AdsConfigsHelper.getConfigParams;


/**
 * Created by Administrator on 2018/1/2.
 */

public class KeyboardBean {

    private boolean mIsInit;

    /**
     * 开关
     */
    private boolean switchStatus;

    /**
     * 安装天数
     */
    private int installDay;

    /**
     * 最大显示次数
     */
    private int maxShowCount;

    /**
     * 间隔
     */
    private int adsSplit;

    private boolean isSim;

    private boolean isWifi;

    private List<String> mAdsPositionList;

    public KeyboardBean() {
        mAdsPositionList = new ArrayList<>();
    }

    public void parse() {
        try {
            int vc = AppUtils.getVersionCodeByPkgName(ApplicationHelper.getContext(), ApplicationHelper.getContext().getPackageName());
            String value = getConfigParams("key_board_info" + vc);
            if (TextUtils.isEmpty(value)) {
                value = getConfigParams("key_board_info");
            }
            if (!TextUtils.isEmpty(value)) {
                LogUtils.d("value : " + value);
                JSONObject jsonObject = new JSONObject(value);
                switchStatus = jsonObject.optBoolean("switch");
                isSim = jsonObject.optBoolean("isSim", true);
                isWifi = jsonObject.optBoolean("isWifi", true);
                installDay = jsonObject.optInt("install");
                maxShowCount = jsonObject.optInt("max_show");
                adsSplit = jsonObject.optInt("split");
                JSONArray jsonArray = jsonObject.optJSONArray("position");
                if (null != jsonArray && jsonArray.length() > 0) {
                    mAdsPositionList.clear();
                    for (int i = 0; i < jsonArray.length(); i++) {
                        mAdsPositionList.add(jsonArray.optString(i));
                    }
                }
                mIsInit = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean isSwitchOpen() {
        if (!mIsInit) {
            parse();
        }
        return switchStatus;
    }

    public boolean isSim() {
        if (!mIsInit) {
            parse();
        }
        return isSim;
    }

    public boolean isWifi() {
        if (!mIsInit) {
            parse();
        }
        return isWifi;
    }

    public int getInstallDay() {
        if (!mIsInit) {
            parse();
        }
        return installDay;
    }

    public int getMaxShowCount() {
        if (!mIsInit) {
            parse();
        }
        return maxShowCount;
    }

    public int getAdsSplit() {
        if (!mIsInit) {
            parse();
        }
        return adsSplit;
    }

    public List<String> getAdsPositionList() {
        if (!mIsInit) {
            parse();
        }
        return mAdsPositionList;
    }
}
